﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cocos2d;
using cocos2d_xna2.Classes;

namespace cocos2d_xna2.Classes
{
    public class Joystick : CCLayer
    {
        CCPoint centerPoint;	//中心点
        CCPoint currentPoint;	//当前位置
        float Radius;			//半径
        bool isActive;			//是否激活
        CCSprite jsSprite;		//摇杆
        CCSprite jsSpriteBg;	//摇杆背景

        tankAction tAct;
        // action button
        CCSprite item_f_n, item_f_s, item_u_out, item_u_on, item_l_out, item_l_on, item_r_out, item_r_on, item_d_out, item_d_on;
        GameLayer glayer;


        public GameLayer Glayer
        {
            get
            {
                return glayer;
            }
            set
            {
                glayer = value;
            }
        }
        //控制地图连续移动
        void keepDoing()
        {
            float distanX = jsSprite.position.x - centerPoint.x;
            float distanY = jsSprite.position.y - centerPoint.y;
            float angle = 361.00f;

            if (distanX > 0 && distanY > 0)
            {
                distanX = Math.Abs(distanX);
                distanY = Math.Abs(distanY);
                angle = (float)(Math.Atan2(distanX, distanY) * 180 / Math.PI);
            }
            else if (distanX < 0 && distanY > 0)
            {
                distanX = Math.Abs(distanX);
                distanY = Math.Abs(distanY);
                angle = (float)(Math.Atan2(distanY, distanX) * 180 / Math.PI);
                angle += 270;
            }
            else if (distanX > 0 && distanY < 0)
            {
                distanX = Math.Abs(distanX);
                distanY = Math.Abs(distanY);
                angle = (float)(Math.Atan2(distanY, distanX) * 180 / Math.PI); ;
                angle += 90;
            }
            else if (distanX < 0 && distanY < 0)
            {
                distanX = Math.Abs(distanX);
                distanY = Math.Abs(distanY);
                angle = (float)(Math.Atan2(distanX, distanY) * 180 / Math.PI);
                angle += 180;
            }
            //偏移度数决定上下左右
            if ((angle < 45 || angle >= 315) && angle <= 360)
            {
                tAct = tankAction.tUp;
            }
            else if (angle >= 45 && angle < 135)
            {
                tAct = tankAction.tRight;
            }
            else if (angle >= 135 && angle < 215)
            {
                tAct = tankAction.tDown;
            }
            else if (angle >= 215 && angle < 315)
            {
                tAct = tankAction.tLeft;
            }
            else
            {
                tAct = tankAction.tStay;
            }
            //获取离中心点的长度，设定移动的步长
            float length = (float)Math.Sqrt(distanX * distanX + distanY * distanY);
            if (length > 0 && length < 20)
            {
                glayer.Tank.MoveStep = 1;
            }
            else if (length >= 20 && length < 40)
            {
                glayer.Tank.MoveStep = 2;
            }
            else if (length > 40 && length < Radius)
            {
                glayer.Tank.MoveStep = 4;
            }
            glayer.onTankAction(tAct);
        }

        //获取摇杆方位
        CCPoint getDirection(){
            return CCPointExtension.ccpNormalize(CCPointExtension.ccpSub(centerPoint, currentPoint));
        }
        float getVelocity(){
            return CCPointExtension.ccpDistance(centerPoint, currentPoint);
        }


        public override bool init()
        {
            if (!base.init())
            {
                return false;
            }
           centerPoint = new CCPoint(80, 80);
           currentPoint = centerPoint;
           Radius = 60;
           jsSprite = CCSprite.spriteWithFile(@"images/cup1");
           jsSpriteBg = CCSprite.spriteWithFile(@"images/js");
           isActive = false;

           jsSprite.position = centerPoint;
           jsSpriteBg.position = centerPoint;

           jsSprite.Opacity = 0;
           jsSpriteBg.Opacity = 0;

           addChild(jsSprite, 2);
           addChild(jsSpriteBg, 1);
           isTouchEnabled = true;
           return true;
        }

        public static new Joystick node()
        {
            Joystick layer = new Joystick();
            if (layer.init())
                return layer;
            return null;
        }

        //public static Joystick JoystickWithArgument(CCPoint aPoint, float radius,
        //               CCSprite ajs, CCSprite ajsBg)
        //{

        //    Joystick newJoystick = new Joystick();
        //    newJoystick.initWithArgument(aPoint, radius,
        //                   ajs, ajsBg);

        //    return newJoystick;


        //}

        //bool initWithArgument(CCPoint aPoint, float radius,
        //                       CCSprite ajs, CCSprite ajsBg)
        //{
        //    if (!base.init())
        //    {
        //        return false;
        //    }
        //    centerPoint = aPoint;
        //    currentPoint = centerPoint;
        //    Radius = radius;
        //    jsSprite = ajs;
        //    jsSpriteBg = ajsBg;
        //    isActive = false;

        //    jsSprite.position = centerPoint;
        //    jsSpriteBg.position = centerPoint;

        //    jsSprite.Opacity = 0;
        //    jsSpriteBg.Opacity = 0;

        //    addChild(jsSprite, 2);
        //    addChild(jsSpriteBg, 1);

        //    return true;

        //}

        public override void ccTouchEnded(CCTouch touch, CCEvent event_)
        {
            jsSprite.runAction(CCFadeTo.actionWithDuration(3, 150));
            jsSpriteBg.runAction(CCFadeTo.actionWithDuration(3, 150));

            currentPoint = centerPoint;

 	        base.ccTouchEnded(touch, event_);
        }

        public override bool ccTouchBegan(CCTouch touch, CCEvent event_)
        {

            CCPoint touchPoint = touch.locationInView(touch.view());
            touchPoint = CCDirector.sharedDirector().convertToGL(touchPoint);

            if (CCPointExtension.ccpDistance(touchPoint, centerPoint) > Radius)
            {
                return false;
            }
            else
            {
                jsSpriteBg.runAction(CCFadeTo.actionWithDuration(0.05f, 255));
                jsSprite.runAction(CCFadeTo.actionWithDuration(0.05f, 255));

                currentPoint = touchPoint;
            }

            return true;
        }

        public override void ccTouchMoved(CCTouch touch, CCEvent event_)
        {
            CCPoint touchPoint = touch.locationInView(touch.view());
            touchPoint = CCDirector.sharedDirector().convertToGL(touchPoint);

            jsSpriteBg.runAction(CCFadeTo.actionWithDuration(0.05f, 255));
            jsSprite.runAction(CCFadeTo.actionWithDuration(0.05f, 255));

            if (CCPointExtension.ccpDistance(touchPoint, centerPoint) > Radius)
            {
                currentPoint = CCPointExtension.ccpAdd(centerPoint, CCPointExtension.ccpMult(CCPointExtension.ccpNormalize(CCPointExtension.ccpSub(touchPoint, centerPoint)), Radius));
            }
            else
            {
                currentPoint = touchPoint;
            }
            base.ccTouchMoved(touch, event_);
        }







        //change CCTime to dt
        void UpDatePos(float dt)
        {
            jsSprite.position = CCPointExtension.ccpAdd(jsSprite.position,
                CCPointExtension.ccpMult(CCPointExtension.ccpSub(currentPoint,jsSprite.position), 0.5f));
            
            keepDoing();
        }
        
        

        public void jsActive()
        {
            if (!isActive)
            {
                isActive = true;

                jsSprite.runAction(CCFadeTo.actionWithDuration(0.5f, 100));
                jsSpriteBg.runAction(CCFadeTo.actionWithDuration(0.5f, 100));
                //添加触摸委托
                CCTouchDispatcher.sharedDispatcher().addTargetedDelegate(this, int.MinValue + 1, true);
                //添加更新函数
                schedule(UpDatePos);
            }
        }

        void jsUnActive()
        {
            if (isActive)
            {
                isActive = false;
                unschedule(UpDatePos);
                CCTouchDispatcher.sharedDispatcher().removeDelegate(this);
            }
        }

    }
}
